package com.hobee.basic.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static java.time.temporal.ChronoUnit.DAYS;

/**
 * @author junjie.li
 * @date 2020/8/15 15:03
 */
public class DateUtils {
    private static final String HM2_PATTERN = "[0-9]{2}:[0-9]{2}";

    public static final String TIME_YMDHMS_PATTERN = "yyyy-MM-dd HH:mm:ss";

    private DateUtils() {
    }

    /**
     * 获取本周开始时间 开始时间从周一开始算起
     * minus(before - 1, DAYS)
     *
     * @return String
     */
    public static String getBeginDayOfWeek(String ymdDate) {
        LocalDate date = LocalDate.parse(ymdDate);
        int before = date.getDayOfWeek().getValue();
        return date.minus(before - 1, DAYS).toString();
    }

    /**
     * 获取本周结束时间
     *
     * @return String
     */
    public static String getEndDayOfWeek(String ymdDate) {
        LocalDate date = LocalDate.parse(ymdDate);
        int before = date.getDayOfWeek().getValue();
        return date.plus(7 - before, DAYS).toString();
    }

    /**
     * 获取当前时间 yyyy-MM-dd HH:mm:ss
     *
     * @return String
     */
    public static String getNow2YmdHms() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(TIME_YMDHMS_PATTERN));
    }

    /**
     * 某段时间内的时间集合
     *
     * @param beginDate 开始时间
     * @param endDate   结束时间
     * @return List
     */
    public static List<String> rangeDate(String beginDate, String endDate) {
        LocalDate begin = LocalDate.parse(beginDate);
        LocalDate end = LocalDate.parse(endDate);

        List<String> list = new ArrayList<>();
        list.add(begin.toString());
        while (begin.isBefore(end)) {
            begin = begin.plus(1, DAYS);
            list.add(begin.toString());
        }
        return list;
    }

    /**
     * 当前天所在周的时间集合
     *
     * @param ymdDate 当前天
     * @return list
     */
    public static List<String> rangeDateOfWeek(String ymdDate) {
        return rangeDate(getBeginDayOfWeek(ymdDate), getEndDayOfWeek(ymdDate));
    }

    /**
     * 又日期转换成 日期 + 星期  eg:11-08(周五)
     *
     * @param ymdDate 需要转换的日期
     * @return string eg:11-08(周五)
     */
    public static String dayAndWeek(String ymdDate) {
        return LocalDate.parse(ymdDate).format(DateTimeFormatter.ofPattern("MM-dd")) + "(" + DayOfWeek.valueOf(LocalDate.parse(ymdDate).getDayOfWeek().name()).getChineseWeek() + ")";
    }

    /**
     * 获取三十天后的日期
     *
     * @return String 日期
     */
    public static String nowAfter30Day() {
        return LocalDate.now().plus(30, DAYS).toString();
    }

    /**
     * 验证小时是否符合规则
     *
     * @param time 来源
     * @return boole
     */
    public static boolean isHhmmValid(String time) {
        return Pattern.matches(HM2_PATTERN, time);
    }

    /**
     * 两个字符串的时间比较大小
     *
     * @param time1 1
     * @param time2 2
     * @return boolean
     */
    public static boolean isBefore(String time1, String time2) {
        return LocalDateTime.parse(time1, DateTimeFormatter.ofPattern(TIME_YMDHMS_PATTERN)).isBefore(LocalDateTime.parse(time2, DateTimeFormatter.ofPattern(TIME_YMDHMS_PATTERN)));
    }

    /**
     * 两个字符串的时间比较大小
     *
     * @param time1 1
     * @return boolean
     */
    public static boolean isBeforeNow(String time1) {
        return LocalDateTime.parse(time1, DateTimeFormatter.ofPattern(TIME_YMDHMS_PATTERN)).isBefore(LocalDateTime.now());
    }

    /**
     * 两个字符串的时间比较大小
     *
     * @param time1 1
     * @return boolean
     */
    public static boolean isAfterNow(String time1) {
        return LocalDateTime.parse(time1, DateTimeFormatter.ofPattern(TIME_YMDHMS_PATTERN)).isAfter(LocalDateTime.now());
    }
}
